package com.example.gkcircle.library.utils;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import com.example.gkcircle.library.LibraryRequest;

import android.util.Log;

/**
 * 
 * @ClassName: StringUtil
 * @Description: TODO(字符串工具类)
 * @author ZhouHua
 * @date 2015年9月4日 下午5:32:19
 *
 */
public class LibraryStringUtil {

	/**
	 * 验证邮箱地址是否正确
	 * 
	 * @param email
	 * @return
	 */
	public static boolean isEmail(String email) {
		boolean flag = false;
		try {
			String check = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
			Pattern regex = Pattern.compile(check);
			Matcher matcher = regex.matcher(email);
			flag = matcher.matches();
		} catch (Exception e) {
			flag = false;
		}
		return flag;
	}

	/**
	 * 
	 * @Title: getUrl
	 * @Description: TODO(拼接Get请求URL)
	 * @param @param url
	 * @param @param params
	 * @param @return 设定文件
	 * @return String 返回类型
	 * @throws
	 */
	public static String getUrl(String url, HashMap<String, String> params) {
		try {

			if (params != null) {
				Iterator<String> it = params.keySet().iterator();
				StringBuffer sb = null;
				while (it.hasNext()) {
					String key = it.next();
					String value = params.get(key);
					if (sb == null) {
						sb = new StringBuffer();
						sb.append("?");
					} else {
						sb.append("&");
					}
					sb.append(key);
					sb.append("=");
					sb.append(URLEncoder.encode(value, "UTF-8"));
				}
				url += sb.toString();
			}
		} catch (UnsupportedEncodingException u) {
			Log.w("GetUrl", "encoding fail");
		}
		Log.i("LibraryStringUtil", "getUrl: "+url);
		return url;
	}

	/**
	 * 
	 * 描述：解析cookie
	 * 
	 * @return String
	 */
	public static String analysisCookie(String cookies) {
		int s = cookies.indexOf(":");
		int e = cookies.indexOf(";");
		cookies = cookies.substring((s + 1), e);
		return cookies.trim();
	}

	/**
	 * 
	 * 描述：解析带等号的cookie
	 * 
	 * @return String
	 */
	public static String analysisCookieWithEquals(String cookies) {
		int s = cookies.indexOf("=");
		int e = cookies.indexOf(";");
		cookies = cookies.substring((s + 1), e);
		return cookies.trim();
	}

	/**
	 * 
	 * @Title: getEmailStatus
	 * @Description: TODO(用户邮箱绑定状态)
	 * @param @param email
	 * @param @return
	 * @return int 0 通过验证 1 未验证 2 未填写
	 * @throws
	 */
	public static int getEmailStatus(String email) {
		int w = email.indexOf("未");
		int y = email.indexOf("已");
		if (w == -1) {
			return 0;
		} else if (y == -1) {
			email = email.substring(0, w);
			if (!isEmail(email.trim().toString())) {
				return 2;
			} else {
				return 1;
			}
		}
		return 0;
	}

	/**
	 * 过滤邮箱
	 * 
	 * @param email
	 * @return 完整的email
	 */
	public static String FilerEmail(String email) {
		int w = email.indexOf("未");
		int y = email.indexOf("已");
		if (w == -1) {
			email = email.substring(0, y);
		} else if (y == -1) {
			email = email.substring(0, w);
			if (!isEmail(email.trim().toString())) {
				email = "未绑定邮箱";
			}
		}
		return email;
	}

	/**
	 * 
	 * 描述：过滤cookie
	 * 
	 * @return String
	 */
	public static String filerCookie(String cookies) {
		int s = cookies.indexOf("=");
		int e = cookies.indexOf(";");
		cookies = cookies.substring((s + 1), e);
		return cookies;
	}

	/**
	 * 
	 * 描述：判断cookie是否过期
	 * 
	 * @return Boolean
	 */
	public static boolean isCookiePast(Document doc) {
		Elements cookiePast = doc.select("h5.box_bgcolor").select("strong");
		if (cookiePast.text().equals("登录我的图书馆")) {
			return true;
		}
		return false;
	}

	/**
	 * 
	 * 描述：判断密码是否修改成功
	 * 
	 * @return Boolean
	 */
	public static boolean hasChangeSuccess(Document doc) {
		Elements cookiePast = doc.select("strong.iconerr");
		if (cookiePast.text().length() == LibraryRequest.CHANGE_PSW_SUCCESS) {
			return true;
		}
		return false;
	}

	/**
	 * 
	 * 描述：过滤用户信息
	 * 
	 * @param info
	 * @return String
	 */
	public static String filerUserInfo(String info) {
		int e = info.indexOf("：");
		info = info.substring((e + 1));
		return info;
	}

	/**
	 * 描述：过滤除数字以外的字符
	 * 
	 * @return String
	 */
	public static String filerOnlyNumber(String text) {
		text = text.trim();
		String str = "";
		if (text != null && !"".equals(text)) {
			for (int i = 0; i < text.length(); i++) {
				if (text.charAt(i) >= 48 && text.charAt(i) <= 57) {
					str += text.charAt(i);
				}
			}
		}
		return str;
	}

}
